<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/20
 * Time: 11:28
 * BannerRepository.php
 */


namespace App\Repositories;


use App\Models\AuctionRule;
use Carbon\Carbon;

class AuctionRuleRepository extends BaseRepositories
{
    /**
     * @var AuctionRule 注入的 model
     */
    protected $auctionRule;

    /**
     * AuctionRuleRepository constructor.
     * @param AuctionRule $auctionRule
     */
    public function __construct(AuctionRule $auctionRule)
    {
        $this->auctionRule = $auctionRule;
    }

    /**
     * @param array $data
     * @param int $limit
     * @return mixed
     */
    public function getAuctionRulePageRepository(array $data, $limit = LIMIT_PAGE){
        return $this->auctionRule
            ->where($data)
            ->paginate($limit);
    }

    /**
     * 更新拍卖规则
     * @param array $data
     * @param int $categoryId
     * @return mixed
     */
    public function updateAuctionRuleRepository(array $data){
        $data['updateTime']=Carbon::now();
        return $this->auctionRule->where('id', $data['id'])->update($data);
    }

    /**
     * 更新拍卖规则状态
     * @param array $data
     * @return mixed
     */
    public function updateAuctionRuleStatusRepository(array $data){
        return $this->auctionRule
            ->where('id', data_get($data, 'id', 0))
            ->update(['status'=>data_get($data, 'status', ''),
                'updateTime'=>Carbon::now()]);
    }
    /**
     * 删除拍卖规则
     * @param int $id
     * @return mixed
     */
    public function deleteAuctionRuleByIdRepository($id){
        return $this->auctionRule->where(['id'=>$id])->delete();
    }

    /**
     * 保存拍卖规则
     * @param array $data
     * @return AuctionRule
     */
    public function addAuctionRuleRepository(array $data){
        //保存拍卖和sku信息
        $auctionInfo = [
            'title'=>data_get($data, 'title', ''),
            'createBy' =>data_get($data, 'createBy', ''),
            'updateBy' =>data_get($data, 'updateBy', ''),
            'ruleKey'=>data_get($data, 'ruleKey', ''),
            'status'=>data_get($data, 'status', 0),
            'content'=>data_get($data, 'content', ''),
            'createTime'=>Carbon::now(),
            'updateTime'=>Carbon::now()
        ];

        return $this->auctionRule->create($auctionInfo);

    }
    /**
     * 获取拍卖规则
     * @param $ruleKey
     * @return mixed
     */
    public function getAuctionRuleByKeyRepository($ruleKey){
        return $this->auctionRule
            ->where(['ruleKey'=>$ruleKey])
            ->orderByDesc('updateTime')
            ->orderByDesc('createTime')
            ->first();
    }
}
